package com.yjs.app.trade.web.controller.base;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;

import com.yjs.app.trade.core.base.exception.BusinessException;
import com.yjs.app.trade.core.util.DESUtils;
import com.yjs.app.trade.core.util.DateUtil;
import com.yjs.app.trade.core.util.JedisUtil;
import com.yjs.app.trade.core.util.JsonDateValueProcessor;
import com.yjs.app.trade.entity.base.BusinessConstant;
import com.yjs.app.trade.entity.base.SysConstant;
import com.yjs.app.trade.web.controller.base.util.ExceptionInterceptorController;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
@Controller
public class BaseController extends ExceptionInterceptorController {

	public static Logger logger = LoggerFactory.getLogger(Logger.class);
	
	public static String userToken = "USERTOKEN";
	
	public static String autType = "AUTTYPE";
	
	public static String baseToken = "org.user.context.";
	
	public static String desKey = "yjs_trade_app";
	
	public static String loginKey = "1";
	
	public String mobileCodeType = "0123456789"; 
	
	public String mobileCodeRedisHead = "sms_";
	
	public static JsonConfig jsonConfig = new JsonConfig();
	
	public String getUserId (HttpServletRequest request) {
		try {
			return JSONObject.fromObject(JSONObject.fromObject(JedisUtil.get(request.getHeader(userToken))).get("allAttribute")).getString("PASSPORT_ID");
		} catch (Exception e) {
			throw new BusinessException(SysConstant.ErrorCode.NO_LOGIN, "缓存已过期,请重新登录");
		}
	}
	
	public String getUserMemberId (HttpServletRequest request) {
		try {
			return JSONObject.fromObject(JSONObject.fromObject(JedisUtil.get(request.getHeader(userToken))).get("allAttribute")).getString("MEMBER_ID");
		} catch (Exception e) {
			throw new BusinessException(SysConstant.ErrorCode.NO_LOGIN, "缓存已过期,请重新登录");
		}
	}
	
	public int getUserMemberType (HttpServletRequest request) {
		try {
			String memberType = JSONObject.fromObject(getUserInfo(request).getJSONArray("appNatures").get(0)).getString("appNatureId");
			return this.getMemberType(memberType);
		} catch (Exception e) {
			throw new BusinessException(SysConstant.ErrorCode.NO_LOGIN, "缓存已过期,请重新登录");
		}
	}
	
	public JSONObject getUserInfo (HttpServletRequest request) {
		try {
			return JSONObject.fromObject(JedisUtil.get(request.getHeader(userToken)));
		} catch (Exception e) {
			throw new BusinessException(SysConstant.ErrorCode.NO_LOGIN, "缓存已过期,请重新登录");
		}
	}
	
	static {
		jsonConfig.registerJsonValueProcessor(Date.class , new JsonDateValueProcessor()); 
	}
	
	public static String packData(Object data) {
		logger.debug(JSONObject.fromObject(data, jsonConfig).toString());
		return DESUtils.getEncryptStr(JSONObject.fromObject(data, jsonConfig).toString(), desKey);
	}
	
	public static JSONObject dpackData(String data) {
		logger.debug("des~~~~~~~~~~~~~:" + data);
		logger.debug(DESUtils.getDecryptStr(data, desKey));
		try {
			return JSONObject.fromObject(DESUtils.getDecryptStr(data, desKey));
		} catch (Exception e) {
			e.printStackTrace();
			throw new BusinessException("UTIL_EXCEPTION", "解析json出错");
		}
	}
	
	public int getMemberType (String memberTypeId) {
		if (memberTypeId.equals(BusinessConstant.MemberType.BUYER)) {
			return BusinessConstant.MemberType.BUYERMEMBER;
		} else if (memberTypeId.equals(BusinessConstant.MemberType.SELLER)) {
			return BusinessConstant.MemberType.SELLERMEMBER;
		} else if (memberTypeId.equals(BusinessConstant.MemberType.DISPATCH)) {
			return BusinessConstant.MemberType.DISPATCHERMEMBER;
		} else if (memberTypeId.equals(BusinessConstant.MemberType.SUPERVISION)) {
			return BusinessConstant.MemberType.SUPERVISIONMEMBER;
		} else {
			return BusinessConstant.MemberType.UNIONMEMBER;
		}
	}
	
	public Map<String, String> getBetweenDate (Object beginDt, Object endDt) {
		Map<String, String> map = new HashMap<String, String>();
		if (beginDt == null) {
			beginDt = "1970-01-01";
		}
		if (endDt == null) {
			endDt = DateUtil.dateFormat.format(new Date());
		}
		map.put("beginDt", beginDt.toString());
		map.put("endDt", endDt.toString());
		return map;
	}
	
	public String changeCollectionName(Map<String,Object> map, boolean isManager, String passportId,String conactionName) {
		//System.out.println("是否主账号"+isManager+" passportId"+passportId+" 未改集合名称"+conactionName);
		//boolean isManager = userService.isManagerPassport(passportId);
		if (!isManager) {
			map.put("passportId", passportId);
			conactionName = conactionName.replace("dispatcher", "dispatcherPassport").replace("seller", "sellerPassport");
		} 
		//System.out.println("改变的集合名称:"+conactionName);
		return conactionName;
	}
}
